//
//  WebViewProgressView.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/20.
//  Copyright © 2018 Mr.wang. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift

class WebViewProgressView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.width = 0
        self.backgroundColor = APPConfig.baseColor
        self.layer.cornerRadius = 1
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension Reactive where Base: WebViewProgressView {
    internal var progress: Binder<Double> {
        return Binder(self.base) { view, progress in
            UIView.animate(withDuration: 0.4) {
                view.width = SCREEN_WIDTH * CGFloat(progress)
            }
        }
    }
    
    internal var loading: Binder<Bool> {
        return Binder(self.base) { view, loading in
            let alpha: CGFloat = loading ? 1.0 : 0.0
            UIView.animate(withDuration: 0.1, animations: {
                view.alpha = alpha
            }, completion: { (_) in
                if !loading {
                    view.width = 0
                }
            })
        }
    }
}
